package com.lianyu507.service.impl;

import com.lianyu507.mapper.CrWordTranslateMapper;
import com.lianyu507.mapper.DtProfessionLogMapper;
import com.lianyu507.mapper.ShUserWordMapper;
import com.lianyu507.pojo.CrWordTranslateExample;
import com.lianyu507.pojo.DtProfessionLog;
import com.lianyu507.pojo.DtProfessionLogExample;
import com.lianyu507.pojo.ShUserWord;
import com.lianyu507.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * =================================
 * <p>
 * Created by Administrator on 2017/11/22.
 * <p>
 * 描述:
 */
@Service
public class DataServiceImpl implements DataService{

    @Autowired
    private ShUserWordMapper shUserWordMapper;

    @Autowired
    private DtProfessionLogMapper dtProfessionLogMapper;

    @Autowired
    private CrWordTranslateMapper crWordTranslateMapper;

    @Override
    public List<DtProfessionLog> selectUserWord() {

        List<ShUserWord> userWords = shUserWordMapper.selectByUserId();
        List<DtProfessionLog> list = new ArrayList<>();
        for(ShUserWord e : userWords){
            DtProfessionLog dtProfessionLog = new DtProfessionLog();
            dtProfessionLog.setLogType(2);
            dtProfessionLog.setLogId(e.getUserId());
            dtProfessionLog.setCount(e.getCount());
            dtProfessionLog.setStatus((byte)1);
            dtProfessionLog.setCreateTime(new Date());
            dtProfessionLog.setUpdateTime(new Date());
            list.add(dtProfessionLog);
        }
		
        return list;
    }

    @Override
    public void insertProfession(List<DtProfessionLog> list) {

        DtProfessionLogExample dtProfessionLogExample = new DtProfessionLogExample();

        for(DtProfessionLog e : list){
            dtProfessionLogExample.createCriteria().andLogTypeEqualTo(2).andLogIdEqualTo(e.getLogId());
            List<DtProfessionLog> dtProfessionLogs =  dtProfessionLogMapper.selectByExample(dtProfessionLogExample);
            if(dtProfessionLogs.size()==0){
                e.setMessage("插入用户单词量");
                dtProfessionLogMapper.insert(e);
            }else{
                Map<String,Object> map=new HashMap<>();
                map.put("count",e.getCount());
                map.put("message","更新用户单词量");
                map.put("updateTime",new Date());
                dtProfessionLogMapper.updateByExampleSelective(map,dtProfessionLogExample);
            }
        }

    }

    @Override
    public Integer selectDayWords() {
        CrWordTranslateExample crWordTranslateExample = new CrWordTranslateExample();
        crWordTranslateExample.createCriteria().andUpdateTimeBetween(startDate(),endDate());
        Integer dayWords=crWordTranslateMapper.countByExample(crWordTranslateExample);
        return dayWords;
    }

    @Override
    public void insertDayWordsToProfession(Integer dayWords) {
        DtProfessionLogExample dtProfessionLogExample = new DtProfessionLogExample();
        dtProfessionLogExample.createCriteria().andLogTypeEqualTo(3).andCreateTimeBetween(startDate(),endDate());
        List<DtProfessionLog> list =  dtProfessionLogMapper.selectByExample(dtProfessionLogExample);
        DtProfessionLog dtProfessionLog = new DtProfessionLog();
        if(list.size() == 0){
            dtProfessionLog.setLogType(3);
            dtProfessionLog.setCount(dayWords);
            dtProfessionLog.setCreateTime(new Date());
            dtProfessionLog.setMessage("插入每天的爬虫单词量");
            dtProfessionLog.setUpdateTime(new Date());
            dtProfessionLog.setStatus((byte) 1);
            dtProfessionLogMapper.insert(dtProfessionLog);
        }else{
            Map<String,Object> map = new HashMap<>();
            map.put("count",dayWords);
            map.put("updateTime",new Date());
            dtProfessionLogMapper.updateByExampleSelective(map,dtProfessionLogExample);
        }
    }

    public Date startDate(){
        Calendar c1 = new GregorianCalendar();
        c1.set(Calendar.HOUR_OF_DAY,0);
        c1.set(Calendar.MINUTE,0);
        c1.set(Calendar.SECOND,0);
        System.out.println(c1.getTime().toLocaleString());
        return c1.getTime();
    }

    public Date endDate(){
        Calendar c2 = new GregorianCalendar();
        c2.set(Calendar.HOUR_OF_DAY,23);
        c2.set(Calendar.MINUTE,59);
        c2.set(Calendar.SECOND,59);
        System.out.println(c2.getTime().toLocaleString());
        return c2.getTime();
    }
}
